

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script type="text/javascript" src="helpman_topicinit.js"></script>

<title>Code Generation - Activity Diagrams [Enterprise Architect User Guide]</title>

<meta name="keywords" content="Enterprise Architect, Sparx Systems, UML, Generate Code,Generate Code From Activity Diagrams,Activity Diagram,Activity Diagram Generate Code From,Code Generation,Code Generation From Activity Diagrams">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />

<link type="text/css" href="default.css" rel="stylesheet" />
<style type="text/css">
  A, A:Visited
  {
    color: #00F;
  }

  A:Active, A:Hover
  {
    color: #F00;
  }

  OL
  {
    margin: 5px 0 5px 48px;
  }

  UL
  {
    margin-bottom: 3px;
    margin-top: 0;
  }

  #nsr
  {
    background-color: #D4DFFF;
  }

  #nsr A
  {
    text-decoration: none;
  }

  #mainbody
  {
    padding: 8px;
  }

  .p_BodyText
  {
    padding: 5px 0;
  }
</style>



<style type="text/css" media="screen">
  BODY
  {
    background-color: #FFFFFF;
  }

  #nsr
  {
    padding: 6px 6px 0 6px;
    border-bottom: none;
    vertical-align: top;
    z-index: 2;
    visibility: visible;
    left: 0;
    top: 0;
    position: absolute;
  }

  #mainbody
  {
    left: 0;
    top: 0;
    margin: 0;
    position: absolute;
    padding: 10px;
    overflow: auto;
    height: 100%;
    z-index: 1;
    background-repeat: no-repeat;
    background-position: bottom right;
    background-attachment: fixed;
  }
</style>

<style type="text/css" media="print">
  #nsr
  {
    visibility: none;
  }

  #mainbody
  {
    overflow: visible;
  }
</style>
<script type="text/javascript" language="JavaScript" src="nonscroll.js"></script>

</head>
<body scroll="no">

<div id="page">



        <div id="content">
          <div id="content-inner">
          



  
          <div id="nsr">
            <table border="0" cellspacing="0" cellpadding="4" width="100%">
              <tr valign="top">
                <td align="left">
  


                  <p class="p_Heading1"><span class="f_Heading1">Code Generation - Activity Diagrams</span></p>



  
                </td>
                <td align="right">
                  <a href="introduction.htm" onmouseover="document.images.main.src='button_main_h.gif'" onmouseout="document.images.main.src='button_main.gif'">
                    <img name="main" src="button_main.gif" border=0 alt="Return to Introduction">
                  </a>&nbsp;
    
                  <a href="code_generation___interaction_.htm" onmouseover="document.images.prev.src='button_prev_h.gif'" onmouseout="document.images.prev.src='button_prev.gif'">
                    <img name=prev src="button_prev.gif" border=0 alt="Previous page" />
                  </a>&nbsp;
    
    
    
                  <a href="codeengineeringsettings.htm" onmouseover="document.images.next.src='button_next_h.gif'" onmouseout="document.images.next.src='button_next.gif'">
                    <img name=next src="button_next.gif" border=0 alt="Next page" />
                  </a>
    
    
                </td>
              </tr>
            </table>
          </div>
          <div id="mainbody">
  


                <p class="p_BodyText"><span class="f_BodyText">Code generation from an <a href="activitydiagram.htm">Activity diagram</a> in a Class requires a validation phase, during which Enterprise Architect uses the system engineering graph optimizer to analyze the diagram and render it into various code-generatable constructs. The constructs are also transformed into one of the various action types (if appropriate), similar to the Interaction diagram constructs. The EASL code generation macros are then used to generate code from these constructs; see the </span><span class="f_BodyText" style="font-style: italic;"><a href="easl_code_generation_macros.htm">EASL Code Generation Macros</a></span><span class="f_BodyText"> topic.</span></p>
<p class="p_BodyText"><span class="f_BodyText">To provide a comprehensive analysis of these features several diagrams from the </span><span class="f_BodyText" style="font-style: italic;">EAExample</span><span class="f_BodyText"> project are shown as examples.</span></p>
<p class="p_SubHeadingL1"><span class="f_SubHeadingL1">Conditional Statements</span></p>
<p class="p_BodyText"><span class="f_BodyText">To model a conditional statement, you use Decision/Merge nodes. Alternatively, you can imply Decisions/Merges internally. The graph optimizer expects an associated Merge node for each Decision node, to facilitate efficient tracking of various branches and analysis of the code constructs within them. </span></p>
<p class="p_BodyText"><span class="f_BodyText">The following diagram is interpreted as a nested IF statement.</span></p>
<p class="p_TextIndentedL2"><img src="do%20validateuser.png" width="573" height="610" border="0" alt="do ValidateUser"></p>
<p class="p_BodyText"><span class="f_BodyText">The Java code that might be generated from this diagram is as follows:</span></p>
<p class="p_BodyText"><span class="f_BodyText">&nbsp;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">public boolean doValidateUser(String Password,String UserName)</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">loadAccountDetails();</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">boolean bRet;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">if (Username==name)</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">if (Password == password)</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">bRet = true;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">bValidUser = true;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">else</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">bRet = false;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"><span class="f_Code">&nbsp;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">else</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">bRet = false;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"><span class="f_Code">&nbsp;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">return bRet;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_SubHeadingL1"><span class="f_SubHeadingL1">Invocation Actions (Call Operation Action, Call Behavior Action) </span></p>
<p class="p_BodyText"><span class="f_BodyText" style="font-style: italic;">Call Actions</span><span class="f_BodyText"> are handled more efficiently. Each action has arguments relating to the parameters of the associated behavior (use the </span><span class="f_MenuKeyField">Synchronize</span><span class="f_BodyText"> button of the </span><span class="f_UIControl">Arguments</span><span class="f_BodyText"> dialog to synchronize arguments and parameters). </span></p>
<p class="p_BodyText"><span class="f_BodyText">The following diagram demonstrates the use of a </span><span class="f_BodyText" style="font-style: italic;">Call Behavior Action</span><span class="f_BodyText"> and a </span><span class="f_BodyText" style="font-style: italic;">Call Operation Action</span><span class="f_BodyText"> interspersed with a conditional statement.</span></p>
<p class="p_TextIndentedL2"><img src="do%20markaccountclosed.png" width="383" height="488" border="0" alt="Do markAccountClosed"></p>
<p class="p_BodyText"><span class="f_BodyText">The generated Java code might appear as follows:</span></p>
<p class="p_BodyText"><span class="f_BodyText">&nbsp;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">public void doMarkAccountClosed()</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">doValidateUser(password,name);</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">if (bValiduser)</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">setClosed(true);</span></p>
<p class="p_Code"><span class="f_Code">&nbsp;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">else</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">System.out.println(&quot;Invalid user&quot;);</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">return;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_SubHeadingL1"><span class="f_SubHeadingL1">Loops</span></p>
<p class="p_BodyText"><span class="f_BodyText">Enterprise Architect's system engineering graph optimizer is also capable of analyzing and identifying loops. An identified loop is internally rendered as an </span><span class="f_BodyText" style="font-style: italic;">Action Loop</span><span class="f_BodyText">, which is translated by the EASL code generation macros to generate the required code. </span></p>
<p class="p_BodyText"><span class="f_BodyText">The following diagram demonstrates how a loop can be modeled. </span></p>
<p class="p_TextIndentedL2"><img src="do%20checkforoutstandingorders.png" width="360" height="610" border="0" alt="do checkForOutstandingOrders"></p>
<p class="p_BodyText"><span class="f_BodyText">The generated Java code might appear as follows:</span></p>
<p class="p_BodyText"><span class="f_BodyText">&nbsp;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">public void doCheckForOutstandingOrders()</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">if (status != closed)</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">initializeStateMachine();</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">while (status != closed)</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">runStateMachine();</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">else</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">{</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">//No Outstanding orders;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">return;</span></p>
<p class="p_Code"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="f_Code">}</span></p>




            </div>
          </div>
        </div>



</div>


</body>
</html>
